package dapps.web.proj;

import java.io.IOException;
import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.web.struts2.Struts2Utils;

import com.opensymphony.xwork2.ActionSupport;

import dapps.entity.proj.Plan;
import dapps.entity.proj.Proj;
import dapps.service.proj.PlanManager;
import dapps.service.proj.ProjManager;

@SuppressWarnings("serial")
public class ProjajaxAction extends ActionSupport implements
		ServletResponseAware {

	@Autowired
	private ProjManager projManager;
	
	@Autowired
	private PlanManager planManager;
	
	private HttpServletResponse response;

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;     
	}
	public String execute() throws Exception {
		HttpServletRequest request = Struts2Utils.getRequest();
		
		String value = (String) request.getParameter("value");
		String id = (String) request.getParameter("id");
		if (StringUtils.isNotBlank(id)){
			String[] params = StringUtils.split(id, '_');
			if(params.length==3){				
				Proj entity = projManager.get(Long.parseLong(params[1]));
				
				Object di = new Object();Method set =null;
				if(params[2].equals("Double")) {
					di = StringUtils.isBlank(value)?null:Double.parseDouble(value);
					set = Proj.class.getMethod(params[0],new java.lang.Class[] { Double.class });
				}
				else if(params[2].equals("Integer")) {
					di = StringUtils.isBlank(value)?null:Integer.parseInt(value);
					set = Proj.class.getMethod(params[0],new java.lang.Class[] { Integer.class });
				}
				else if(params[2].equals("Long")) {
					di = StringUtils.isBlank(value)?null:Long.parseLong(value);
					set = Proj.class.getMethod(params[0],new java.lang.Class[] { Long.class });
				}
				else if(params[2].equals("String")) {
					di = StringUtils.isBlank(value)?null:value;
					set = Proj.class.getMethod(params[0],new java.lang.Class[] { String.class });
				}
				else if(params[2].equals("select")) {
					di = Integer.parseInt(value);
					set = Proj.class.getMethod(params[0],new java.lang.Class[] { Integer.class });
					if((Integer)di==1) value = "*";else value="";
				}	
				set.invoke(entity, new Object[] { di });
				if(entity.getForced()==0){
					if(params[0].equals("setJys")||params[0].equals("setKy")||params[0].equals("setSj")||params[0].equals("setXz")
							||params[0].equals("setYdxk")||params[0].equals("setYdys")){
						
						if(projManager.isSs(entity)){
							if(entity.getProrank()>0){
								Integer maxplan = planManager.getMaxPlaned(entity.getYeartime());
								if(entity.getYeartime()<=maxplan ){
									entity.setYeartime(maxplan+1);
									value+="<script>alert('刷新页面后,本项目将自动划入下一期计划"+entity.getYeartime()+"');color('setTitle_"+params[1]+"_String'"+"); </script>";
								}else{
									value+="<script>alert('刷新页面后,本项目将自动划入实施项目库');color('setTitle_"+params[1]+"_String'"+"); </script>";
								}
								entity.setProrank(0);//实施
							}						
						}			
						else if(projManager.isCb(entity)){
							if(entity.getProrank()!=2){
								entity.setProrank(2);//储备
								value+="<script>alert('刷新页面后,本项目将自动划入储备项目库');color('setTitle_"+params[1]+"_String'"+"); </script>";
							}
						}			
						else{
							if(entity.getProrank()!=1){
								entity.setProrank(1);//预备
								value+="<script>alert('刷新页面后,本项目将自动划入预备项目库');color('setTitle_"+params[1]+"_String'"+"); </script>";
							}
						}
					}
				}
				projManager.saveProj(entity);
				response.setContentType("text/xml; charset=utf-8");
				response.setHeader("Cache-Control", "no-cache");
				try {
					response.getWriter().write(value);
					response.getWriter().flush();
				} catch (IOException e) {			
				}
				
			}
			
		}
		
		return NONE;
	}

	public String force() throws Exception {
		HttpServletRequest request = Struts2Utils.getRequest();
				
		String idstr = (String) request.getParameter("id");
		String forceprorankstr = (String) request.getParameter("forceprorank");
		String value="";
		
		if (StringUtils.isNotBlank(idstr)&&StringUtils.isNotBlank(forceprorankstr)){
			Long eid =  Long.parseLong(idstr);
			Proj entity = projManager.get(eid);
			int fprojrank = Integer.parseInt(forceprorankstr);
			String[] tempv = {"<script>alert('刷新页面后,本项目将自动划入实施项目库');color('setTitle_"+idstr+"_String'"+"); </script>",
							  "<script>alert('刷新页面后,本项目将自动划入预备项目库');color('setTitle_"+idstr+"_String'"+"); </script>",
							  "<script>alert('刷新页面后,本项目将自动划入储备项目库');color('setTitle_"+idstr+"_String'"+"); </script>",
							  "<script>alert('刷新页面后,本项目将自动划入省留项目库');color('setTitle_"+idstr+"_String'"+"); </script>",
							  "<script>alert('刷新页面后,本项目将被删除');color('setTitle_"+idstr+"_String'"+"); </script>"};
			if(fprojrank<3){
				Integer maxplan = planManager.getMaxPlaned(entity.getYeartime());
				entity.setProrank(fprojrank);
				entity.setForced(1);
				entity.setSl(0);
				if(fprojrank==0&&entity.getYeartime()<=maxplan ){
					entity.setYeartime(maxplan+1);
					value="<script>alert('刷新页面后,本项目将自动划入下一期计划实施项目库"+entity.getYeartime()+"');color('setTitle_"+idstr+"_String'"+"); </script>";
				}else{
					value = tempv[fprojrank];
				}				
			}else if(fprojrank==4){
				entity.setSl(0);
				entity.setForced(0);
				if(projManager.isSs(entity)){
					if(entity.getProrank()>0){
						Integer maxplan = planManager.getMaxPlaned(entity.getYeartime());
						if(entity.getYeartime()<=maxplan ){
							entity.setYeartime(maxplan+1);
							value="<script>alert('刷新页面后,本项目将自动划入下一期计划"+entity.getYeartime()+"');color('setTitle_"+idstr+"_String'"+"); </script>";
						}else{
							value=tempv[0];
						}
						entity.setProrank(0);//实施
					}						
				}			
				else if(projManager.isCb(entity)){
					if(entity.getProrank()!=2){
						entity.setProrank(2);//储备
						value=tempv[2];
					}
				}			
				else{
					if(entity.getProrank()!=1){
						entity.setProrank(1);//预备
						value=tempv[1];
					}
				}
				entity.setForced(0);
			}else if(fprojrank==3){
				entity.setSl(1);
				entity.setProrank(3);
				entity.setForced(2);
				value=tempv[3];
			}else if(fprojrank==5){//删除
				if(entity.getJzfrom()!=null&&entity.getJzfrom()!=0l){
					Proj pfrom =projManager.get(entity.getJzfrom());
					if(pfrom!=null) {
						pfrom.setJzto(0l);
						projManager.save(pfrom);
					}					
				}
				entity.setProrank(9);
				entity.setForced(1);
				value=tempv[4];
			}else if(fprojrank==6){//结转
				Proj newentity = new Proj();
				PropertyUtils.copyProperties(newentity, entity);				
				newentity.setType(entity.getType());
				newentity.setId(null);	
				Integer newyeartime = (newentity.getYeartime()/100+1)*100+1;				
				Plan p = planManager.getPlan("yeartime", newyeartime);
				if(p!=null&&p.getStatus()<5&&p.getStatus()>0){
					newentity.setYeartime(newyeartime);
					newentity.setApplicate(newentity.getYeartime()*10);
					newentity.setJzfrom(eid);
					value=
					  "<script>alert('刷新页面后,本项目将被结转至"+newyeartime+"');color('setTitle_"+idstr+"_String'"+"); </script>";
					projManager.saveProj(newentity);
					entity.setJzto(newentity.getId());
				}else{
					if(p!=null&&p.getStatus()>=5) value=newyeartime+"计划已下达";
					else value=newyeartime+"计划申报未开始或计划已下达";
				}
				
			}
			projManager.saveProj(entity);	
		}else{
			value="失败";
		}
		
		response.setContentType("text/plain; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		try {
			response.getWriter().write(value);
			response.getWriter().flush();
		} catch (IOException e) {}
		return NONE;

	}

}
